package com.tsingteng.cosfun.ui.message.geneal;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterFragment;
import com.tsingteng.cosfun.bean.DataBean;
import com.tsingteng.cosfun.bean.FollowBean;
import com.tsingteng.cosfun.bean.PlayBean;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.ui.cosfun.ShareFragment;
import com.tsingteng.cosfun.ui.cosfun.TipOffsActivity;
import com.tsingteng.cosfun.ui.message.CommonActivity;
import com.tsingteng.cosfun.ui.message.geneal.presenter.PlayContract;
import com.tsingteng.cosfun.ui.message.geneal.presenter.PlayPresenter;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.ShareUtils;
import com.tsingteng.cosfun.utils.TimeUtils;
import com.tsingteng.cosfun.utils.VideoUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.TitleView;
import com.tsingteng.cosfun.widget.dialog.BottomListDialog;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;

/**
 * 描述 :便当
 * 创建人: 肖利.
 * 时间:2018/8/7
 * qq(872179966)
 */

public class PlayFragment extends BasePresenterFragment<PlayPresenter, PlayContract.IPlayView> implements PlayContract.IPlayView, PlayAdapter.OnAdapterFunctionListener, TitleView.OnTitleClickListener, BottomListDialog.OnClickBottomListListener, ShareFragment.OnShareItemClickLisenter, ShareUtils.ShareListener
        , BackListener {
    @BindView(R.id.tv_view)
    TitleView tvView;
    @BindView(R.id.rv_fan_rv)
    MyRecycleView rvFan;
    private ImageView imageView;
    private int page = 1;
    private long mTotal;
    private PlayBean.PlayListBean item;
    private TextView greatNum;
    private BottomListDialog bottomListDialog;
    private List<String> list;
    private int id;
    private ShareFragment shareFragment;
    private PlayBean.PlayListBean playListBean;
    private TextView shareView;
    private long mStartTime;
    private long mEndTime;
    private TextView mTvAtt;
    private ImageView mIvAtt;


    public static PlayFragment newInstance() {
        PlayFragment fragment = new PlayFragment();
        return fragment;
    }


    @Override
    protected void refresh() {
        page = 1;
        mPresenter.getPlayData(page, Config.PAGE_SIZE);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getResources().getString(R.string.playment));
        mPresenter.getPlayData(1, Config.PAGE_SIZE);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getResources().getString(R.string.playment));
    }

    @Override
    protected void initData(View rootView) {
        mStartTime = TimeUtils.getCurTimeLong();
        shareFragment = new ShareFragment();
        tvView.setTitle("便当");
        tvView.setOnTitleClickListener(this);
        tvView.setIvLeft(R.drawable.back_big_icon);
        tvView.setIvRight(R.drawable.write_paragraph_icon);
        bottomListDialog = new BottomListDialog(getContext());
        list = new ArrayList<>();
        list.add("举报");
        list.add("不感兴趣");
        bottomListDialog.setList(list);
        bottomListDialog.setOnClickBottomListListener(this);
    }

    @Override
    protected PlayPresenter createPresenter() {
        return new PlayPresenter();
    }

    @Override
    protected View initHeader() {
        return null;
    }

    @Override
    protected MyRecycleView createRecycleView() {
        return rvFan;
    }

    @Override
    protected PlayAdapter createAdapter() {
        PlayAdapter playAdapter = new PlayAdapter();
        playAdapter.setOnAdapterFunctionListener(this);
        return playAdapter;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (adapter.getData() == null || adapter.getData().size() == 0) return;
        PlayBean.PlayListBean playListBean = (PlayBean.PlayListBean) adapter.getData().get(position);
        id = playListBean.getId();
        String name = playListBean.getNickName();
        int profileId = playListBean.getProfileId();
        int relationType = playListBean.getRelationType();
        Navigate.startPlayDetails(getContext(), id, name, profileId, relationType);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        mPresenter.getPlayData(page, Config.PAGE_SIZE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fans;
    }


    @Override
    public void showPlayData(PlayBean data) {
        if (rvFan != null) rvFan.refreshComplete();
        if (data == null || data.getData() == null) {
            if (1 == page) {
                showToast(getResources().getString(R.string.data_no));
            }
            return;
        }
        mTotal = data.getTotal();
        if (page == 1) {
            mAdapter.getData().clear();
        }
        mAdapter.addData(data.getData());
        mAdapter.notifyDataSetChanged();
        if (page >= data.getTotalPages()) {
            showToast(getResources().getString(R.string.no_more));
            mAdapter.loadMoreEnd();
        } else {
            mAdapter.loadMoreComplete();
        }

    }

    @Override
    public void showPraiseResult(Integer data) {
        if (data == 1) {
            item.setPraiseStatus(1);
            int num = Integer.valueOf(item.getPraiseCount()) + 1;
            item.setPraiseCount(String.valueOf(num));
            greatNum.setText(String.valueOf(num));
            showToast("点赞成功!");
            imageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.great_pink_icon));
        } else {
            int num = Integer.valueOf(item.getPraiseCount()) - 1;
            item.setPraiseCount(String.valueOf(num));
            greatNum.setText(String.valueOf(num));
            imageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.great_icon));
            showToast("取消点赞成功!!");
            item.setPraiseStatus(0);
        }
    }

    @Override
    public void getFollowResult(FollowBean followBean) {
        int relationType = followBean.getRelationType();
        if (relationType == 0) {
            mTvAtt.setText("关注");
            mIvAtt.setBackground(getActivity().getResources().getDrawable(R.drawable.shape_bg_white));
            mTvAtt.setTextColor(getActivity().getResources().getColor(R.color.black));
        } else if (relationType == 1) {
            mTvAtt.setText("已关注");
            mIvAtt.setBackground(getActivity().getResources().getDrawable(R.drawable.shape_trank_bg));
            mTvAtt.setTextColor(getActivity().getResources().getColor(R.color.white));
        } else if (relationType == 2) {
            mTvAtt.setText("相互关注");
            mIvAtt.setBackground(getActivity().getResources().getDrawable(R.drawable.shape_pink_bg));
            mTvAtt.setTextColor(getActivity().getResources().getColor(R.color.color_found));
        } else if (relationType == -1) {
//            rl_guan_zhu.setVisibility(View.INVISIBLE);
        }
        item.setRelationType(relationType);
    }

    @Override
    public void showDelResult(String status) {
        showToast("删除成功!!");
        page = 1;
        mPresenter.getPlayData(page, Config.PAGE_SIZE);
    }

    @Override
    public void ShowNotLiketResult(String status) {
        showToast("已标记为不感兴趣!");
        page = 1;
        mPresenter.getPlayData(page, Config.PAGE_SIZE);
    }

    @Override
    public void onMoreClick(PlayBean.PlayListBean item) {
        playListBean = item;
        list.clear();
        if (item.getProfileId() == LogonUtils.getProFildId()) {
            list.add("删除");
        } else {
            list.add("举报");
            list.add("不感兴趣");
        }
        bottomListDialog.setList(list);
        bottomListDialog.show();
    }

    @Override
    public void onAttentionClick(PlayBean.PlayListBean item, TextView textView, ImageView imageView) {
        this.item = item;
        this.mTvAtt = textView;
        this.mIvAtt = imageView;
        if (item.getRelationType() ==0) {
            mPresenter.loginFollowFans(item.getProfileId() + "");
        }else if (item.getRelationType()==1) {
            mPresenter.loginNoFollow(item.getProfileId() + "");
        }else if (item.getRelationType()==2) {
            mPresenter.loginNoFollow(item.getProfileId() + "");
        }

}

    @Override
    public void onFilmDrawClick(PlayBean.PlayListBean item) {
//        showToast("onFilmDrawClick");
        VideoHelper.getIntence().starVideoShootToPlay(getActivity(), item.getId());
    }


    @Override
    public void onGreatClick(PlayBean.PlayListBean item, ImageView imageView, TextView greatNum) {
        this.imageView = imageView;
        this.item = item;
        this.greatNum = greatNum;
        if (item.getPraiseStatus() == 1) {
            mPresenter.getPraiseOrNotData("-1", String.valueOf(item.getId()), "0");
        } else {
            mPresenter.getPraiseOrNotData("1", String.valueOf(item.getId()), "0");
        }
    }

    @Override
    public void onCommentClick(PlayBean.PlayListBean item) {
        showToast("onCommentClick");
    }

    @Override
    public void onShareClick(PlayBean.PlayListBean item, TextView shareView) {
        this.item = item;
        this.shareView = shareView;
        if (shareFragment == null) shareFragment = new ShareFragment();
        shareFragment.setLisenter(this);
        shareFragment.setTag("play");
        shareFragment.show(getActivity().getSupportFragmentManager(), "playDetailFragment");
    }

    @Override
    public void onLeft() {
//        getActivity().finish();
        mEndTime = TimeUtils.getCurTimeLong();
        mPresenter.getPlayLongTime(String.valueOf(mStartTime), String.valueOf(mEndTime - mStartTime));
    }

    @Override
    public void onRight() {
        Navigate.startCommont(getContext(), CommonActivity.KEY_ADD_PLAY);
//        Navigate.startDrafts(getContext());
    }

    @Override
    public void onRight2() {

    }

    public PlayPresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void onClickItem(BaseQuickAdapter adapter, View view, int position, List<String> list) {
        int workId = playListBean.getId();
//        int id = data.getProfileId();
        switch (list.get(position)) {
            case "举报":
                int type = 5;
                Navigate.startReportList(getContext(), type, workId);
                bottomListDialog.dissMiss();
                break;
            case "不感兴趣":
                mPresenter.getAddNotLikePlay(workId + "");
                bottomListDialog.dissMiss();
                break;
            case "删除":
                new AlertDialog.Builder(getContext()).setTitle("提示").setMessage("确定删除本便当么？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mPresenter.getDelPlay(workId + "");
                    }
                }).setNegativeButton("取消", null).create().show();
                bottomListDialog.dissMiss();
                break;
        }
    }

    @Override
    public void onClickBottom() {

    }

    @Override
    public void share(SHARE_MEDIA share_media, int type) {
        showLoading("正在分享...");
        ShareUtils.getIntance().sharePlay(getActivity(), share_media, String.valueOf(item.getId()), item.getNickName() + ShareUtils.shareTitle, item.getContent(), item.getImage(), this);
    }

    @Override
    public void other(int type) {
        switch (type) {
            case 1://复制链接
                ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                String path = "";
                ClipData myClip = ClipData.newPlainText("text", path);
                cm.setPrimaryClip(myClip);
                Toast.makeText(getActivity(), "链接已经复制到剪切板", Toast.LENGTH_SHORT).show();
                break;
            case 3://不感兴趣
                showToast("不感兴趣");
//                mPresenter.getLoseInterest(opusBean.getId() + "", System.currentTimeMillis() + "");
                break;
            case 4:
                Toast.makeText(getActivity(), "举报", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), TipOffsActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onShareFinish() {
        hideLoading();
    }

    @Override
    public void onShareSuccess() {
        hideLoading();
        item.setForwardCount(String.valueOf(Integer.valueOf(item.getForwardCount()) + 1));
        shareView.setText(item.getForwardCount());
    }

    @Override
    public void showLongTime(DataBean status) {
        Objects.requireNonNull(getActivity()).finish();
    }

    @Override
    public void showTimeFail() {
        Objects.requireNonNull(getActivity()).finish();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof CommonActivity) {
            ((CommonActivity) context).setBackListener(this);
            ((CommonActivity) context).setInterception(true);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (getActivity() instanceof CommonActivity) {
            ((CommonActivity) getActivity()).setBackListener(null);
            ((CommonActivity) getActivity()).setInterception(false);
        }
    }

    @Override
    public void backListener() {
        mEndTime = TimeUtils.getCurTimeLong();
        mPresenter.getPlayLongTime(String.valueOf(mStartTime), String.valueOf(mEndTime - mStartTime));
    }
}
